﻿using System;
using System.Collections.Generic;
using System.Text;
using StarBotDotNet;

namespace StarBotDotNet
{
    public class UnitManager 
    {
        UnitDictionary currentUnits = new UnitDictionary();
        List<UnitParty> parties = new List<UnitParty>();
        public UnitManager()
        {
        }

        public void AddParty(UnitParty party)
        {
            parties.Add(party);
        }

        public List<UnitParty> Parties
        {
            get { return parties; }
        }



        public class UnitEventArgs:EventArgs
        {
            UnitData affectedUnit;

            public UnitData AffectedUnit
            {
                get { return affectedUnit; }
            }

            public UnitEventArgs(UnitData affectedUnit)
            {
                this.affectedUnit = affectedUnit;
            }
        }



        public static UnitDictionary CreateUnitDictionary(System.Collections.ArrayList units, int currentTime)
        {
            UnitDictionary currentUnits = new UnitDictionary();
            foreach (Unit unit in units)
            {
                if (!currentUnits.ContainsKey(unit.Type))
                {
                    currentUnits.Add(unit.Type, new SortedDictionary<int, UnitData>());
                }

                SortedDictionary<int, UnitData> unitList = currentUnits[unit.Type];
                unitList.Add(unit.ID, new UnitData(unit, currentTime));
            }
            return currentUnits;
        }


        internal void RunUnitManager(System.Collections.ArrayList arrayList, int currentTime, ProxyBot bot)
        {
            //Created or found units
            List<UnitData> discoveredUnits = new List<UnitData>();
            List<UnitData> dissapearedUnits = new List<UnitData>();
            UnitDictionary newUnits = CreateUnitDictionary(arrayList, currentTime);
            newUnits.ForEach(delegate(UnitData unit)
            {
                if (!currentUnits.ContainsUnit(unit.Type, unit.ID))
                {
                    discoveredUnits.Add(unit);
                    currentUnits.Add(unit, null);
                }
                else
                {
                    currentUnits[unit.Type][unit.ID].UpdateUnit(unit.Unit, currentTime);
                }

                
            });

            currentUnits.ForEach(delegate(UnitData unit)
            {
                if (!newUnits.ContainsUnit(unit.Type, unit.ID))
                {
                    dissapearedUnits.Add(unit);
                    if (!currentUnits.NotAssigned(unit))
                    {
                        currentUnits.GetParty(unit).Remove(unit);
                        currentUnits.SetParty(unit, null);
                    } 
                }


            });


            parties.Sort();
            //UnitDictionary unusedUnits = FindUnusedUnits(unitToParties);




            foreach (UnitParty party in parties)
            {
                party.PopulateWithUnits(bot, currentUnits, discoveredUnits, dissapearedUnits, currentTime);
                party.ExecuteTactics(bot, currentUnits, currentTime);
            }

            //unitToParties = newUnitsToParties;
            //currentUnits = newUnits;
        }


    }
}
